package com.educhains.web.ctrl.frontend;


import com.alibaba.fastjson.JSONObject;
import com.educhains.module.activity.service.ActivityService;
import com.educhains.module.activity.vo.mb_model.Activity;
import com.educhains.module.blog.service.BlogService;
import com.educhains.module.blog.vo.Blog;
import com.educhains.module.post.service.PostService;
import com.educhains.module.post.vo.Post;
import com.educhains.module.user.service.UserService;
import com.educhains.module.user.vo.User;
import com.educhains.web.util.JsonUtils;
import com.educhains.web.util.SessionUtils;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class CommunityCtrl {

    @Autowired
    private PostService postService;
    @Autowired
    private BlogService blogService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private UserService userService;


    @RequestMapping(value = "/community", method = RequestMethod.GET)
    public String index(HttpServletRequest request) {

        List<Post> posts = getPost("new");
        List<Blog> blogs = getBlog("new");
        List<Activity> activities = activityService.selectActivityListLimit(5, "");
        List<User> users = userService.selectUserListLimit(5, "");

        User sessionUser = SessionUtils.getUser(request);

        request.setAttribute("posts", posts);
        request.setAttribute("blogs", blogs);
        request.setAttribute("activities", activities);
        request.setAttribute("users", users);
        request.setAttribute("userInfo", sessionUser);


        return "frontend/community/community";
    }

    @RequestMapping(value = "/community/type", method = RequestMethod.POST)
    public String communityPost(HttpServletRequest request) {
        String block = request.getParameter("block");

        String type = request.getParameter("type");
        if(block.equals("post")) {
            List<Post> posts = getPost(type);
            request.setAttribute("posts", posts);
            return "frontend/community/blockList/postBlockList";
        }
        if(block.equals("blog")) {
            List<Blog> blogs = getBlog(type);
            request.setAttribute("blogs", blogs);
            return "frontend/community/blockList/blogBlockList";
        }


//        User sessionUser = SessionUtils.getUser(request);
//        request.setAttribute("userInfo", sessionUser);

        return "404";
    }


    private List<Blog> getBlog(String type) {
        if(type.equals("new")) {
            return  blogService.selectBlogListLimit(5, "new");
        }
        if(type.equals("hot")) {
            return blogService.selectBlogListLimit(5, "hot");
        }
        if(type.equals("essence")) {
            return blogService.selectBlogListLimit(5, "essence");
        }
        return null;
    }

    private List<Post> getPost(String type) {
        if(type.equals("new")) {
            return  postService.selectPostListLimit(5, "new");
        }
        if(type.equals("hot")) {
            return postService.selectPostListLimit(5, "hot");
        }
        if(type.equals("essence")) {
            return postService.selectPostListLimit(5, "essence");
        }
        return null;
    }




    private static String returnBody(boolean success, String msg) {
        JSONObject json = new JSONObject();
        if (success) {
            json.put("success", true);
            json.put("msg", msg);
        } else {
            json.put("success", false);
            json.put("msg", msg);
        }
        //return json.toJSONString();
        return JsonUtils.toJSONString(json);
    }

}
